﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlServerCe;


namespace legends
{
    public partial class WorldEditForm : Form
    {
        //
        // Property containing informations about screen resolution, difficulty, fonts etc...
        //
        private GameConstants gameConstants;

        //
        // Property containing tiles which will be added to tilesPanel
        //
        private List<TileButton> tileButtons= new List<TileButton>();

        //
        // Property containing x and y coordinates of top left tile in currently desplayed map
        //
        private Position currentPosition;

        //size of tiles in pixels, 40 x 40
        private int tileSize = 40;                  

        //testing instance
        World world = new World("Opatovice", 100, 100, new Position(10, 10));

        #region initializingForm
        //
        //constructor 
        //@gameConst> form "inherits" gameConstants from MainForm
        public WorldEditForm(GameConstants gameConst)
        {
                
            InitializeComponent();

           

            gameConstants = gameConst;                         //loading gameConstants from previous Form, needed for setting resolution                    
            this.ClientSize = new System.Drawing.Size(gameConstants.getResWidth(), gameConstants.getResHeight());   //setting forms size
            currentPosition = world.GetPosition();             //sets starting position (position of tile in top left corner)
            
            
            InitializeTilesPanel(getTilesFromDatabase());
            FormLoad();
        
        }

        //
        // Placement of forms components
        //  
        private void FormLoad()
        {
            TilePropertiesPanel.Width = Convert.ToInt16(gameConstants.getResWidth() / 1.2);
            TilePropertiesPanel.Height = gameConstants.getResHeight() / 4;
            TilePropertiesPanel.Top = gameConstants.getResHeight() - TilePropertiesPanel.Height - 20;
            TilePropertiesPanel.Left = buttonsPanel.Width + 40;

            TilesPanel.Width = gameConstants.getResWidth() / 4;
            TilesPanel.Left = gameConstants.getResWidth() - TilesPanel.Width;
            TilesPanel.Top = 20;
            TilesPanel.Height = gameConstants.getResHeight() - TilePropertiesPanel.Height - 20;


            WorldMapBox.Left = buttonsPanel.Size.Width + 20;
            WorldMapBox.Top = 20;
            int pom = Convert.ToInt16(Decimal.Round((gameConstants.getResWidth() - TilesPanel.Width - 50 - buttonsPanel.Width) / 40));
            int pom2 = Convert.ToInt16(Decimal.Round((gameConstants.getResHeight() - TilePropertiesPanel.Height - 50) / 40));
            gameConstants.setMapSize(pom, pom2);            //sets size of map in tiles according to placing of components in form
            WorldMapBox.Width = pom * 40 + 1;
            WorldMapBox.Height = pom2 * 40 + 1; 
        }
        #endregion

        //
        // Method loads tiles to tilesPanel from database
        //
        private void InitializeTilesPanel(DataTableReader dtr)
        {
           Converter converter = new Converter();           //instance of converter from database String to TileType Enum
           while (dtr.Read())
                {

              
               tileButtons.Add(new TileButton(new Button(), new Tile(dtr["Name"].ToString(),converter.ConvertTileType(dtr["TileType"].ToString()), dtr["Image"].ToString())));

               //     int bla = dtr.GetInt16(0);
               //     String str = dtr.GetString(2);
                }
            int pom = 0;
            foreach (TileButton tb in tileButtons)
            {
                
                {
                    tb.getButton().Location = new Point(0, pom * 40 + 5);
                    tb.getButton().Text = tb.getTile().getName();

                    tb.getButton().Image = Image.FromFile(Environment.CurrentDirectory + "\\graphics\\tiles\\" + tb.getTile().getImage());               
                //    natureTabPage.Controls.Add(tb.getButton());
                    pom++;
                }
                switch (tb.getTile().getTileType())
                {
                    case TileType.Nature: natureTabPage.Controls.Add(tb.getButton()); break;
                    default: break;
                }

            }
         



        }
        private DataTableReader getTilesFromDatabase()
        {
            SqlCeConnection sqlConnection = new SqlCeConnection(Properties.Settings.Default.Database1ConnectionString);
            sqlConnection.Open();
            SqlCeDataAdapter sqlDataAdapter = new SqlCeDataAdapter("Select * from Tiles", sqlConnection);
            DataTable dataTable = new DataTable("States");
            sqlDataAdapter.Fill(dataTable);
            DataTableReader datatableReader = dataTable.CreateDataReader();
            sqlConnection.Close();


            return datatableReader;
        }

        #region drawing

        //
        // Method painting map tiles to PictureBox
        //
        private void WorldMapBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black);
            g.Clear(Color.White);
            g.DrawRectangle(pen, 0, 0, tileSize * (gameConstants.getMapWidth()), tileSize * (gameConstants.getMapHeight()));
            
            //Draws tiles on map 
            for (int i = 0; i < gameConstants.getMapWidth(); i++)
                for (int j = 0; j < gameConstants.getMapHeight(); j++)
                {
                    Image newImage = Image.FromFile(world.GetTileImage(i,j));
                    //Image ni = Application.StartupPath();
                    g.DrawImage(newImage, tileSize * (i + 1), tileSize * (j + 1));               
                
                }

            //Draws horizontal strings on MapPictureBox
            for (int i = 0; i < gameConstants.getMapWidth(); i++)
            {
                g.DrawString((i+currentPosition.getPosX()).ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(Color.Blue), (i + 1) * tileSize, 0);
            }
            //Draws vertical strings on MapPictureBox
            for (int i = 0; i < gameConstants.getMapHeight(); i++)
            {
                g.DrawString((i+currentPosition.getPosY()).ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(Color.Blue), 0, (i + 1) * tileSize);
            }

            //draws rectangles around tiles on MapPictureBox
            for (int i = 0; i < gameConstants.getMapWidth(); i++)
                 for (int j = 0; j < gameConstants.getMapHeight(); j++)
                 {
                     g.DrawRectangle(pen, (i + 1) * tileSize, (j + 1) * tileSize, tileSize, tileSize);
                 }
        }
        #endregion

        #region buttons
        //
        // Opens TileEditorForm, closes this form
        //
        private void TileEditorButton_Click(object sender, EventArgs e)
        {
            TileEditorForm tileEditorForm = new TileEditorForm(gameConstants);

            tileEditorForm.Show();
            this.Visible = false;
        }

        //
        // Opens ItemEditorForm, closes this form
        //
        private void ItemEditorButton_Click(object sender, EventArgs e)
        {
            ItemEditorForm itemEditorForm = new ItemEditorForm(gameConstants);

            itemEditorForm.Show();
            this.Visible = false;
        }

        //
        // Closes application
        //
        private void ExitButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }
        #endregion 
    }

    //
    // Class binding tile to button. These objects are placed in tilesPanel
    //
    public class TileButton
    {
        private Tile tile;
        private Button button;

        public TileButton(Button button, Tile tile)
        {
            this.button = button;
            this.tile = tile;
        }

        public Button getButton()
        {
            return this.button;
        }

        public Tile getTile()
        {

            return this.tile;
        }
    }
}
